package biz;

import bean.*;
import java.util.List;

public class ShowBiz {
	
	public static String HistoryResult(int iid){
		String curtag = "&nbsp;&nbsp;<span class=\"alert\">当前版本！</span>";
		List<VItem> list = ItemBiz.getEditHistory(iid);
		Item item = ItemBiz.getItemByIid(iid);
		String ret ="<h3><a href=\"item.jsp?iid="+item.getIid()+"\">"+item.getIname()+"</a>的编辑历史</h3><hr/>";
		if(list.isEmpty()){
			return "抱歉，在数据库中没有找到词条" + item.getIname() + "<span>&nbsp;[词条编号:"+item.getIid()+"]</span>的编辑历史！";
		}else{
			ret += "<p style=\"font-size:12px;font-family:Verdana, Arial, Helvetica, sans-serif, SimHei;\">词条\"" + item.getIname() + "\"共有" + list.size() + "个编辑历史版本。<span>&nbsp;[词条编号:"+item.getIid()+"]</span></p><hr/>";
			
			ret += "<div style=\"padding-left:4em\">";
			for (VItem vitem :list){
				ret += "<div class=\"rootitem\">\n";
				//显示版本信息
					ret += "<div style=\"font-size:14px;font-family:Arial, Helvetica, sans-serif;text-align:right\">";
					if (item.getCurvid() == vitem.getVid()) ret += curtag;
					ret += "词条版本编号："+vitem.getVid();
					ret += "</div>";
					java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						String tt = sdf.format(vitem.getCreatetime());
						ret	+= "<div style=\"font-size:12px;font-family:Arial, Helvetica, sans-serif;text-align:right\">"
							+"版本创建时间:" 
							+ tt
							+ "&nbsp;|&nbsp;版本创建人:<a href=\"mailto:" + UserBiz.getUserByUid(vitem.getUid()).getUemail() + "\">" 
						+ UserBiz.getUserByUid(vitem.getUid()).getUname() + "</a>";
						ret += "</div>"
						+"<hr/>\n";
				//显示词条当前内容
					ret += "<div class=\"rootiteminfo\">\n";
					ret += vitem.getInfo();
					ret += "</div><hr/><br/>\n";
				ret += "</div>\n";
			}
			ret += "</div>";
		}
		return ret;
	}
	
	public static String SearchResult(String key){
		String ret = "";
		Search s = new ItemBiz();
		List<Item> list = s.SolidSearch(key);
		if(list.isEmpty()){
			return "抱歉，在数据库中没有找到相关词条<br/>您可以点击<a href=\"additem.jsp?l=0&p=0&r=0\">这里</a>创建您需要的词条";
		}else{
			ret += "<p style=\"font-size:12px;font-family:Verdana, Arial, Helvetica, sans-serif, SimHei;\">关键词" + key + "共找到" + list.size() + "个相关词条</p><hr/>";
			ret += "<div style=\"padding-left:4em\">";
			for (Item item :list){
				ret += "<div class=\"rootitem\">\n";
				//显示词条名
				ret += "<div class=\"rootitemname\">\n";
				ret += "<a href=\"item.jsp?iid=" + item.getIid() + "\">";
				ret += item.getIname();
				ret += "</a>" + "<div style=\"font-size:12px;font-family:Arial, Helvetica, sans-serif;text-align:right\">最后更新时间:" +ItemBiz.getUpdateTime(item) + "</div></div><hr/>\n";
				//显示词条当前内容
				ret += "<div class=\"rootiteminfo\">\n";
				String info = ItemBiz.getInfoByItem(item);
				if (info.length()>200) info = info.substring(0, 200)+"...";
				ret += info;
				ret += "</div><br/><br/>\n";
				ret += "</div>\n";
			}
			ret += "</div>";
		}
		return ret;
	}
		
	public static String ItemtoHtml(Item item){
		int fontsize=0;
		int paddingleft=2*item.getLevel();
		String edit="edititem.jsp?iid="+item.getIid();
		String addchild="additem.jsp?l="+(item.getLevel()+1)+"&p="+item.getIid()+"&r="+item.getRoot();
		String history = "history.jsp?iid="+item.getIid();
		boolean hr=false;
		String s = "";
		switch(item.getLevel()){
		case 0:
			fontsize=24;
			hr=true;
			break;
		case 1:
			fontsize=20;
			hr=true;
			break;
		case 2:
			fontsize=18;
			hr=true;
			break;
		default:
			fontsize=14;
			hr=true;
			break;
		}
		s+="<div style=\"padding-left:"+paddingleft+"em;\">\n";
		s+="<table border=\"0\">\n";
		s+="<tr><td>\n";
		s+="<span class=\"\" style=\"font-size:"+fontsize+"px;font-family:SimHei;\">\n";
		s+="<a name=\""+item.getIid()+"\">\n";
		s+=item.getIname();
		s+="</a></span></td><td style=\"font-size:12px;color:#cccccc;\" align=\"right\" valign=\"bottom\" >\n";
		s+="<a href=\""+addchild+"\">";
		s+="[添加新子项]</a>\n";
		s+="<a href=\""+edit+"\">";
		s+="[编辑]</a>\n";
		s+="<a href=\""+history+"\">";
		s+="[查看编辑历史]</a>\n";
		s+="</td></tr></table>";
		if(hr) s+="<hr/>";
		s+="<div>";
		s+=ItemBiz.getInfoByItem(item);
		s+="</div>";
		s+="</div><br/>\n";
		return s;
	}
	
	public static String ItemListtoHtml(List<Item> list){
		String s = "";
		if (list.isEmpty()) return s;
		for (Item i:list){
			s+=ItemtoHtml(i);
		}
		return s;
	}
	
	public static String ItemListtoLink(List<Item> list){
		String s = "";
		int [] a = new int[20];
		int curlevel=-1;
		a[0]=0;
		for (Item i : list){
			String link = "#"+i.getIid();
			if (i.getLevel()>curlevel) { a[curlevel+1]=0;curlevel = i.getLevel();}
			else if (i.getLevel()<curlevel) {curlevel=i.getLevel();}
			a[curlevel]++;
			String no = "";
			for (int j=1;j<=curlevel;j++) no+=a[j]+".";
			s += "<li><a href=\"" + link + "\">" + no + " " + i.getIname() + "</a></li>";
		}
		
		return s;
	}
	
	public static String RootItemtoHtml(List<Item> list){
		if (list.isEmpty()) return "";
		
		String html ="";
		html += "<div class=\"rootlist\">\n";
		for (Item i : list){
			html += "<div class=\"rootitem\">\n";
			//显示词条名
			html += "<div class=\"rootitemname\">\n";
			html += "<a name=\"" + i.getIid() +"\"></a>";
			html += "<a href=\"item.jsp?iid=" + i.getIid() + "\">";
			html += i.getIname();
			html += "</a>" + "<div style=\"font-size:12px;font-family:Arial, Helvetica, sans-serif;text-align:right\">最后更新时间:" +ItemBiz.getUpdateTime(i) + "</div></div><hr/>\n";
			//显示词条当前内容
			html += "<div class=\"rootiteminfo\">\n";
			html += ItemBiz.getInfoByItem(i);
			html += "</div><br/><br/>\n";
			html += "</div>\n";
		}
		html += "</div>\n";
		
		return html;
	}
	
	public static String RootListtoLink(List<Item> list){
		if (list.isEmpty()) return "";
		String html ="";
		int i=0;
		for (Item item : list){
			String link = "#"+item.getIid();
			i++;
			html += "<li><a href=\"" + link + "\">" + i + ". " + item.getIname() + "</a></li>";
		}
			
		return html;
	}
}
